<template>
  <!-- 角色表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="850px"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="150px"
      :inline="true"
    >
      <el-form-item label="设定项目排序" prop="sequenceQueue">
        <el-input
          v-model="formData.sequenceQueue"
          placeholder="请输入设定项目排序"
          clearable
        />
      </el-form-item>
      <el-form-item label="标准设定项目编号" prop="settingItemCode">
        <el-input
          v-model="formData.settingItemCode"
          placeholder="标准设定项目编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测项目名称" prop="checkItemName">
        <el-input
          v-model="formData.checkItemName"
          placeholder="请输入检测项目名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测类目">
        <el-tree-select
          v-model="formData.checkCategoryId"
          :multiple="false"
          check-strictly
          :props="{
            value: 'id',
            label: 'checkCategoryName',
            children: 'nodes',
          }"
          @change="treeChange"
          :data="categoryList"
          clearable
        />
      </el-form-item>
      <el-form-item label="检测方法" prop="checkItemMethod">
        <el-input
          v-model="formData.checkItemMethod"
          placeholder="请输入检测方法"
          clearable
        />
      </el-form-item>
      <el-form-item label="使用工具" prop="checkItemTool">
        <el-input
          v-model="formData.checkItemTool"
          placeholder="请输入使用工具"
          clearable
        />
      </el-form-item>
      <el-form-item label="是否必输项" prop="isRequired">
        <el-select
          v-model="formData.isRequired"
          placeholder="请选择是否必输项"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('YesNo')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查方式" prop="checkType">
        <el-select
          v-model="formData.checkType"
          placeholder="请选择检查方式"
          clearable
        >
          <el-option
            v-for="item in useDictionary('CheckType')"
            :key="item.value"
            :label="item.label"
            :value="Number(item.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标准值范围上限" prop="standardMax">
        <el-input-number
          :step="0.01"
          v-model="formData.standardMax"
          placeholder="请输入标准值范围上限"
        />
      </el-form-item>
      <el-form-item label="标准值范围下限" prop="standardMin">
        <el-input-number
          :step="0.01"
          v-model="formData.standardMin"
          placeholder="请输入标准值范围下限"
        />
      </el-form-item>
      <el-form-item label="是否录入数值" prop="inputValue">
        <el-select
          v-model="formData.inputValue"
          placeholder="请选择是否录入数值"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('YesNo')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否检查数值" prop="checkValue">
        <el-select
          v-model="formData.checkValue"
          placeholder="请选择是否检查数值"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('YesNo')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" style="width: 750px">
        <el-input
          type="textarea"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictionary } from "@/utils/useTable";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MDMCHECKSTANDARDSETTINGITEMADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});
let categoryList = ref([]);
let categoryData = ref();
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  checkStandardSettingId: [
    { required: true, message: "请输入检测标准设定id", trigger: "blur" },
  ],
  settingItemCode: [
    { required: true, message: "请输入标准设定项目编号", trigger: "blur" },
  ],
  sequenceQueue: [
    { required: true, message: "请输入设定项目排序", trigger: "blur" },
  ],
  checkItemName: [
    { required: true, message: "请输入检测项目名称", trigger: "blur" },
  ],
  checkCategoryId: [
    { required: true, message: "请输入检测类目id", trigger: "blur" },
  ],
  checkCategoryName: [
    { required: true, message: "请输入检测类目名称", trigger: "blur" },
  ],
  checkItemMethod: [
    { required: true, message: "请输入检测方法", trigger: "blur" },
  ],
  checkItemTool: [
    { required: true, message: "请输入使用工具", trigger: "blur" },
  ],
  isRequired: [
    { required: true, message: "请输入是否必输项", trigger: "blur" },
  ],
  checkType: [{ required: true, message: "请输入检查方式", trigger: "blur" }],
  standardMax: [
    { required: true, message: "请输入标准值范围上限", trigger: "blur" },
  ],
  standardMin: [
    { required: true, message: "请输入标准值范围下限", trigger: "blur" },
  ],
  inputValue: [
    { required: true, message: "请输入是否录入数值", trigger: "blur" },
  ],
  checkValue: [
    { required: true, message: "请输入是否检查数值", trigger: "blur" },
  ],
  version: [{ required: true, message: "请输入数据版本号", trigger: "blur" }],
  remarks: [{ required: true, message: "请输入备注", trigger: "blur" }],
  createTime: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  createdUserId: [{ required: true, message: "请输入创建人", trigger: "blur" }],
  createdUserName: [
    { required: true, message: "请输入创建人名", trigger: "blur" },
  ],
  lastUpdateTime: [
    { required: true, message: "请输入更新时间", trigger: "blur" },
  ],
  lastUpdatedUserId: [
    { required: true, message: "请输入更新人", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { required: true, message: "请输入更新人名", trigger: "blur" },
  ],
  deleted: [{ required: true, message: "请输入删除标识", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  schemeId: "",
  schemeCode: "",
  schemeName: "",
  checkStandardSettingId: "",
  settingItemCode: "",
  sequenceQueue: "",
  checkItemName: "",
  checkCategoryId: "",
  checkCategoryName: "",
  checkItemMethod: "",
  checkItemTool: "",
  isRequired: "",
  checkType: "",
  standardMax: "",
  standardMin: "",
  inputValue: "",
  checkValue: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    const {
      id,
      schemeId,
      schemeCode,
      schemeName,
      checkStandardSettingId,
      settingItemCode,
      sequenceQueue,
      checkItemName,
      checkCategoryId,
      checkCategoryName,
      checkItemMethod,
      checkItemTool,
      isRequired,
      checkType,
      standardMax,
      standardMin,
      inputValue,
      checkValue,
      version,
      remarks,
      createTime,
      createdUserId,
      createdUserName,
      lastUpdateTime,
      lastUpdatedUserId,
      lastUpdatedUserName,
      deleted,
    } = newVal;
    Object.assign(formData, {
      id,
      schemeId,
      schemeCode,
      schemeName,
      checkStandardSettingId,
      settingItemCode,
      sequenceQueue,
      checkItemName,
      checkCategoryId,
      checkCategoryName,
      checkItemMethod,
      checkItemTool,
      isRequired,
      checkType,
      standardMax,
      standardMin,
      inputValue,
      checkValue,
      version,
      remarks,
      createTime,
      createdUserId,
      createdUserName,
      lastUpdateTime,
      lastUpdatedUserId,
      lastUpdatedUserName,
      deleted,
    });
    console.log("formData44444444444", formData);
    if (newVal?.id) {
      getCategoryData(formData.checkCategoryId);
    } else {
      getCategoryList(formData.schemeId);
    }
  }
);
function treeChange() {
  getCategory(formData.checkCategoryId);
}
/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.checkStandardSettingId = "";
  formData.settingItemCode = "";
  formData.sequenceQueue = "";
  formData.checkItemName = "";
  formData.checkCategoryId = "";
  formData.checkCategoryName = "";
  formData.checkItemMethod = "";
  formData.checkItemTool = "";
  formData.isRequired = "";
  formData.checkType = "";
  formData.standardMax = "";
  formData.standardMin = "";
  formData.inputValue = "";
  formData.checkValue = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 检测标准设定项目保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mdm/checkstandardsettingitem/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

/** 检测标准设定项目查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/mdm/checkstandardsettingitem/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}
/** 检测标准设定项目查询详细表 */
async function getCategory(id: string) {
  const data = await request({
    url: "/mdm/checkcategory/get/" + id,
    method: "get",
  });
  formData.checkCategoryName = data.data.checkCategoryName;
  console.log("formData.checkCategoryName", formData.checkCategoryName);
}
onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
/** 检测方案查询详细表 */
async function getCategoryList(id: string) {
  if (id === undefined || id === "") return;
  const data = await request({
    url: "/mdm/checkcategory/getList/" + id,
    method: "get",
  });
  categoryList.value = data.data;
}
async function getCategoryData(id: string) {
  if (id === undefined || id === "" || id === null) return;
  const data = await request({
    url: "/mdm/checkcategory/getListById/" + id,
    method: "get",
  });
  categoryList.value = data.data;
}
</script>
